<template>
  <div id="main">
    <h2>教室管理</h2>
    <div class="head">
      <el-button
        style="margin:20px"
        type="primary"
        @click="handleOpen('添加教室', 1)"
      >添加教室</el-button>
      <el-input
        v-model="searchTableInfo"
        placeholder="请输入搜索内容"
        style="width:240px;float: right;margin:20px;"
      />
    </div>
    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column label="序号" type="index" align="center">
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" prop="roomInfo" label="房间信息" />

        <el-table-column align="center" prop="roomLoc" label="房间地址" />

        <el-table-column align="center" prop="roomStatus" label="房间状态">
          <template slot-scope="Scope">
            <span
              v-if="Scope.row.roomStatus == 1"
              style="color:  rgb(14, 189, 14);font-weight: 800;"
            >可使用</span>
            <span
              v-if="Scope.row.roomStatus == 0"
              style="color: red;font-weight: 800;"
            >维护中</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作">
          <template slot-scope="Scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="handleOpen('修改教室', 2, Scope.row.roomId)"
            >编辑</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(Scope.row.roomId, Scope.row.roomLoc)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5, 10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
    >
      <el-form ref="room" :model="room" label-position="top" :rules="rules">
        <el-form-item
          prop="roomLoc"
          label="教室位置"
          :label-width="formLabelWidth"
        >
          <el-input v-model="room.roomLoc" auto-complete="off" />
        </el-form-item>

        <el-form-item
          prop="roomInfo"
          label="教室信息"
          :label-width="formLabelWidth"
        >
          <el-input v-model="room.roomInfo" auto-complete="off" />
        </el-form-item>

        <el-form-item
          prop="roomStatus"
          label="教室状态"
          :label-width="formLabelWidth"
        >
          <el-select v-model="room.roomStatus" placeholder="请选择">
            <el-option
              v-for="r in roomStat"
              :key="r.value"
              :label="r.label"
              :value="r.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose">取 消</el-button>
        <el-button
          v-if="editType == 2"
          type="primary"
          @click="handleUpdate('room')"
        >修 改</el-button>
        <el-button
          v-if="editType == 1"
          type="primary"
          @click="handleAddRoom('room')"
        >添 加</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  addRoom,
  getAllRoom,
  getRoomById,
  editRoom,
  delRoom
} from '@/api/room'
export default {
  data() {
    return {
      pagesize: 5,
      currentPage: 1,
      // list: [],
      tableList: [],
      searchTableInfo: '',
      listLoading: false,
      dialogFormVisible: false,
      room: [],
      roomStat: [
        {
          value: 0,
          label: '维护中'
        },
        {
          value: 1,
          label: '可使用'
        }
      ]
    }
  },
  computed: {
    // 根据计算属性模糊搜索
    list() {
      const searchTableInfo = this.searchTableInfo
      console.log(this.searchTableInfo)
      if (searchTableInfo) {
        // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
        // 注意： filter() 不会对空数组进行检测。
        // 注意： filter() 不会改变原始数组。
        return this.tableList.filter(data => {
          console.log('success' + data)
          // some() 方法用于检测数组中的元素是否满足指定条件;
          // some() 方法会依次执行数组的每个元素：
          // 如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测;
          // 如果没有满足条件的元素，则返回false。
          // 注意： some() 不会对空数组进行检测。
          // 注意： some() 不会改变原始数组。
          return Object.keys(data).some(key => {
            // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
            // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
            return (
              String(data[key])
                .toLowerCase()
                .indexOf(searchTableInfo) > -1
            )
          })
        })
      }
      return this.tableList
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    fetchData() {
      this.listLoading = true
      getAllRoom().then(data => {
        this.tableList = data.obj
        this.listLoading = false
      })
    },
    handleDelete(id, loc) {
      this.$confirm('确认删除' + loc + '的教室？').then(() =>
        delRoom(id).then(data => {
          this.$notify({
            title: '操作成功',
            type: 'success'
          })
          const totalPage = Math.ceil(
            (this.tableList.length - 1) / this.pagesize
          )
          const page =
            this.currentPage > totalPage ? totalPage : this.currentPage
          this.currentPage = this.currentPage < 1 ? 1 : page
          this.fetchData()
        })
      )
    },
    handleUpdate(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.room)
          editRoom(this.room).then(resp => {
            console.log(resp)
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleOpen(title, type, id) {
      this.room = {}
      this.dialogFormVisible = true
      this.title = title
      this.editType = type
      if (type === 2) {
        console.log(id)
        this.this_stuid = id
        getRoomById(id).then(resp => {
          console.log(resp)
          this.room = resp.obj
        })
      } else {
        console.log('这是孩子信息？', this.room)
      }
    },
    handleAddRoom(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          addRoom(this.room).then(resp => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })

            this.fetchData()
          })
          this.room = {}
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleClose() {
      this.dialogFormVisible = false
      this.room = {}
    },
    getAllRoom: function() {
      getAllRoom().then(res => {
        this.room = res.obj
      })
    }
    // roomSta: function(row, column) {
    //   if (row.roomStatus === 0) {
    //     return '维护中'
    //   }
    //   if (row.roomStatus === 1) {
    //     return '可使用'
    //   }
    // }
  }
}
</script>

<style scoped>
#main {
  margin-left: 5px;
  margin-right: 5px;
}
#table-area {
  margin-left: 10px;
  margin-right: 10px;
}
</style>
